home *** CD-ROM | disk | FTP | other *** search
- Path: news.informatik.uni-muenchen.de!usenet
- From: Kurt Watzka <watzka@stat.uni-muenchen.de>
- Newsgroups: comp.lang.c
- Subject: Re: about free()
- Date: Wed, 28 Feb 1996 15:40:31 +0100
- Organization: Institut fⁿr Statistik
- Message-ID: <3134695F.55E@stat.uni-muenchen.de>
- NNTP-Posting-Host: pc7.stat.uni-muenchen.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Chenyang Xu wrote:
- >
- > Hi, there,
- >
- > I have a simple question here.
- >
- > I have a function say newarrary() which mallocs an array inside and
- > returns a pointer to this arrary. In another function foo.c, the result
- > of newarray() is assigned to a pointer ptr. Now after some operations on
- > this array. I use free(ptr) to free the memory allocated by newarrary().
- > My question is whould this cause a problem, since free() is not freeing
- > the same pointer which is allocated by the malloc() although this
- > pointer point to the same memory.
-
- 1.) You should _never_ free a pointer that was not allocated through one
- of the memory allocation function.
-
- 2.) Whether you use the variable that held the "pointer" returned from the
- allocation function or another variable that _holds_ _the_ _same_
- _value_ does not matter at all. Like all other C functions, free()
- will get it's argument passed by value, so it has access to the "value"
- of your pointer, not to the variable that holds this value.
-
- Kurt
-